<template>
    <ElTooltip placement="right" :disabled="disabledShowTooltip">
        <template #content> {{ fileName }}</template>
        <template #default>
            <div class="file-display-container"
                @mouseenter="showClose = showCloseButton" @mouseleave="showClose = false" :class="{pointer:props.pointer}" @click="handleClick">
                <div class="file-icon">
                    <img :src="getFileIcon(fileType)" :alt="`${fileType}文件图标`">
                </div>
                <div class="file-name" :title="fileName" ref="fileName">
                    {{ fileName }}
                </div>
                <button v-if="showClose && showCloseButton" class="close-button" @click="handleClose">
                    ×
                </button>
            </div>
        </template>
    </ElTooltip>
</template>

<script setup>
import { ElTooltip } from 'element-plus';
import { ref, useTemplateRef, computed } from 'vue';

const props = defineProps({
    fileName: {
        type: String,
        required: true
    },
    fileType: {
        type: String,
        required: true
    },
    customWidth: {
        type: String,
        default: ''
    },
    pointer: {
        type: Boolean,
        default: false
    },
    showCloseButton: {
        type: Boolean,
        default: false
    }
});

const emit = defineEmits(['close','click']);
const fileNameRef = useTemplateRef('fileName');
const disabledShowTooltip = computed(() => !(fileNameRef.value?.scrollWidth > fileNameRef.value?.clientWidth));

const showClose = ref(false);

const fileIcons = {
    pdf: new URL('@/assets/images/fileicon/icons-pdf.svg', import.meta.url).href,
    json: new URL('@/assets/images/fileicon/icons-json.svg', import.meta.url).href,
    // xls: '/icons/xls-icon.png',
    // ppt: '/icons/ppt-icon.png',
    // txt: '/icons/txt-icon.png',
    default: new URL('@/assets/images/fileicon/icons-pdf.svg', import.meta.url).href,
};

const getFileIcon = (type) => {
    //   return new URL('@/assets/images/fileicon/'+(fileIcons[type.toLowerCase()] || fileIcons.default), import.meta.url).href
    return fileIcons[type.toLowerCase()] || fileIcons.default;
};

const handleClose = () => {
    emit('close');
};

const handleClick = ()=>{
    emit('click')
}
</script>

<style scoped>
.pointer {
  cursor: pointer;
}
.file-display-container {
    position: relative;
    display: flex;
    align-items: center;
    padding: 8px 12px;
    border-radius: 12px;
    background: #FFFFFF;
    transition: all 0.3s ease;
    border: 1px solid #EBEBEB;
    box-sizing: border-box;
    height: 56px;
    max-width: 320px;
}

.file-icon {
    margin-right: 10px;
}

.file-icon img {
    width: 24px;
    height: 24px;
}

.file-name {
    flex: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 14px;
}

.close-button {
    position: absolute;
    top: -4px;
    right: -4px;
    width: 20px;
    height: 20px;
    border-radius: 16px;
    border: none;
    background: #fff;
    color: #999;
    cursor: pointer;
    font-size: 20px;
    line-height: 1;
    padding: 0;
    box-shadow: 0px 1px 2px 0px rgba(0, 0, 0, 0.08);
}

.close-button:hover {
    /* color: #ff4d4f; */
}
</style>
